package org.openslash.util;


import android.database.Cursor;

public class AResultList implements ResultList{

	private Cursor cursor;
	private boolean hasnext;
	private int cols = 0;
	
	public AResultList(Cursor cursor)
	{
		this.cursor = cursor;
		this.hasnext = false;
		this.cols = cursor.getColumnCount();
	}

	public ResultRow getNextRow() {
		ResultRow rr = null;
		if( hasnext )
		{
			rr = new ResultRow(cols);
			for(int i = 0; i < cols; ++i)
			{
				rr.addColumn(cursor.getString(i));
			}
		}
		return rr;
	}

	public boolean hasNext() {
		hasnext = cursor.moveToNext();
		if( hasnext == false )
		{
			cursor.close();
		}
		return hasnext;
	}

	public ResultRow getFirstRowAndClose() {
		ResultRow rr = null;
		if( cursor.moveToFirst() )
		{
			rr = new ResultRow(cols);
			for(int i = 0; i < cols; ++i)
			{
				rr.addColumn(cursor.getString(i));
			}
			cursor.close();
		}
		return rr;
	}

	@Override
	public int getColumnCount() {
		return cols;
	}

}
